package zozopo.warehouse;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import zozopo.background.operator.operations.OperatorTB;
import zozopo.background.operator.operations.OperatorlimitedTB;
import zozopo.database.BusiDatabaseManager;
import zozopo.database.DatabaseManager;
import zozopo.flow.comm.Flow;
import zozopo.flow.comm.FlowCode;

import zozopo.warehouse.operations.GoodsOS;
import zozopo.warehouse.operations.GoodsTB;
import zozopo.warehouse.operations.PurchaseOS;
import zozopo.warehouse.operations.PurchaseTB;
import zozopo.warehouse.operations.PurchaselistOS;
import zozopo.warehouse.operations.PurchaselistTB;

import zozopo.warehouse.operations.WarehouseOS;
import zozopo.warehouse.operations.WarehouseTB;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class AddPurchaselistAction extends ActionSupport implements Action,SessionAware{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static final String limitedCode ="WAREHOUSE.PURCHASELIST.ADD";
	
	//传入参数
	private String supplier_purchaselist = "";

	private String supplierinfo_purchaselist = "";
	
	private int warehouseid_purchaselist = 0;

	private double shipprice_purchaselist = 0.00;
	private int goodsnumber_purchaselist = 0;
	private double total_purchaselist = 0.00;
	

	private String remark_purchaselist = "";

	
	private String purchaseInfo = "";
	
	//传出参数
	private PurchaselistTB purchaselist;
	private List<PurchaseTB> list;
	private String operationResult;
	
	private Map<Integer,GoodsTB> goodsInfoMap;
	
	private boolean sqlError = false;
	
	private Map<Object, Object> session;
	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		session = arg0;
	}
	
	
	public String execute(){
		
		//权限处理
		OperatorTB operator = (OperatorTB)session.get("operatorInfo"); 
		//用户权限
		Map<String,OperatorlimitedTB> operatorlimitedMap = (Map<String,OperatorlimitedTB>)session.get("operatorlimitedMap");
		
		if(operator == null){
			addFieldError("errormessage", "用户未登陆或超时.请登陆后操作.");
			return NONE;
		}
	
		
		DatabaseManager databaseManager = new BusiDatabaseManager();
		Connection conn = databaseManager.getConnection();
		
	
		try {
			conn.setAutoCommit(false);

			PurchaseOS purchaseOS = new PurchaseOS(conn);
			PurchaselistOS purchaselistOS = new PurchaselistOS(conn);	
			GoodsOS goodsOS = new GoodsOS(conn);
			
			WarehouseOS warehouseOS = new WarehouseOS(conn);
			WarehouseTB warehouse = warehouseOS.getWarehouseById(warehouseid_purchaselist);
			
			purchaselist = new PurchaselistTB();
			purchaselist.setGoodsnumber_purchaselist(goodsnumber_purchaselist);
			purchaselist.setRemark_purchaselist(remark_purchaselist);
			purchaselist.setShipprice_purchaselist(shipprice_purchaselist);
			purchaselist.setSupplier_purchaselist(supplier_purchaselist);
			purchaselist.setSupplierinfo_purchaselist(supplierinfo_purchaselist);
			purchaselist.setTotal_purchaselist(total_purchaselist);
			purchaselist.setUseremail_purchaselist(operator.getEmail_operator());
			purchaselist.setUsername_purchaselist(operator.getName_operator());
			
			if(!purchaselistOS.insertAndLocked(purchaselist)){
				sqlError = true;
			}
			
			int maxId = purchaselistOS.getMaxIdByEmail(operator.getEmail_operator());
			purchaselist = purchaselistOS.getObjById(maxId);
			List<PurchaseTB> purchaseInfoList = purchaseOS.purchaseInfoToReceiptList(purchaseInfo,purchaselist.getId_purchaselist(),warehouseid_purchaselist,warehouse.getName_warehouse(),operator);
			
			if(!purchaseOS.insert(purchaseInfoList,false,false)){
				sqlError = true;
			}
			
			//receiptlistOS.upadteForProfit(receiptlist);
			
			list = purchaseOS.getListByPurchaselistId(maxId);
			
			/* in Str 组成*/
			String inStr = "(";
			boolean isFirst = true;
		
			for(PurchaseTB purchase:list){
				if(isFirst){
					inStr += purchase.getGoodsid_purchase();
					isFirst = false;
				}else{
					inStr += "," + purchase.getGoodsid_purchase();
				}
			}
			inStr +=")";
			/* in Str 组成  END*/
			
			goodsInfoMap = goodsOS.getMapByInstr(inStr);
			
			//加入流程
			Flow flow = new Flow(conn);
			if(!flow.executeFlow(FlowCode.WAREHOUSE_PURCHASELIST, purchaselist.getId_purchaselist(),null, operator, operatorlimitedMap,"")){
				sqlError = true;
			}
			
			if(sqlError){
				throw new SQLException();
			}
			
			
			conn.commit();
			conn.setAutoCommit(true);
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			operationResult = "插入失败";
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}catch (Exception e) 
		{
			operationResult = "插入失败";
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally{
			databaseManager.releaseConnection(conn);
		}
		
		
	
		
		return SUCCESS;//*/
		
	}
	
	public void validate() {
		//字符议常处理.
		supplierinfo_purchaselist = supplierinfo_purchaselist.replaceAll("'", "\\\\'");
		supplier_purchaselist = supplier_purchaselist.replaceAll("'", "\\\\'");
		remark_purchaselist = remark_purchaselist.replaceAll("'", "\\\\'");
		
		//权限处理
		OperatorTB operator = (OperatorTB)session.get("operatorInfo"); 
	
		if(operator == null){
			addFieldError("errormessage",getText("operator.timeoutornotlogin"));
			return ; 
		} 
		
		//用户权限
		Map<String,OperatorlimitedTB> operatorlimitedMap = (Map<String,OperatorlimitedTB>)session.get("operatorlimitedMap");
		
		if(operatorlimitedMap.get(limitedCode) == null){
			addFieldError("errormessage","用户未有进货权限.");
			return;
		}
		if(supplier_purchaselist.trim().equals("")){//如果供应商为空
			addFieldError("errormessage","供应商不能为空.");
			return;
		}
		
		
	}


	public String getSupplier_purchaselist() {
		return supplier_purchaselist;
	}


	public void setSupplier_purchaselist(String supplier_purchaselist) {
		this.supplier_purchaselist = supplier_purchaselist;
	}


	public String getSupplierinfo_purchaselist() {
		return supplierinfo_purchaselist;
	}


	public void setSupplierinfo_purchaselist(String supplierinfo_purchaselist) {
		this.supplierinfo_purchaselist = supplierinfo_purchaselist;
	}


	public double getShipprice_purchaselist() {
		return shipprice_purchaselist;
	}


	public void setShipprice_purchaselist(double shipprice_purchaselist) {
		this.shipprice_purchaselist = shipprice_purchaselist;
	}


	public int getGoodsnumber_purchaselist() {
		return goodsnumber_purchaselist;
	}


	public void setGoodsnumber_purchaselist(int goodsnumber_purchaselist) {
		this.goodsnumber_purchaselist = goodsnumber_purchaselist;
	}


	public double getTotal_purchaselist() {
		return total_purchaselist;
	}


	public void setTotal_purchaselist(double total_purchaselist) {
		this.total_purchaselist = total_purchaselist;
	}


	public String getRemark_purchaselist() {
		return remark_purchaselist;
	}


	public void setRemark_purchaselist(String remark_purchaselist) {
		this.remark_purchaselist = remark_purchaselist;
	}


	public String getPurchaseInfo() {
		return purchaseInfo;
	}


	public void setPurchaseInfo(String purchaseInfo) {
		this.purchaseInfo = purchaseInfo;
	}


	public PurchaselistTB getPurchaselist() {
		return purchaselist;
	}


	public void setPurchaselist(PurchaselistTB purchaselist) {
		this.purchaselist = purchaselist;
	}


	public List<PurchaseTB> getList() {
		return list;
	}


	public void setList(List<PurchaseTB> list) {
		this.list = list;
	}


	public String getOperationResult() {
		return operationResult;
	}


	public void setOperationResult(String operationResult) {
		this.operationResult = operationResult;
	}




	public int getWarehouseid_purchaselist() {
		return warehouseid_purchaselist;
	}


	public void setWarehouseid_purchaselist(int warehouseid_purchaselist) {
		this.warehouseid_purchaselist = warehouseid_purchaselist;
	}


	public static String getLimitedcode() {
		return limitedCode;
	}


	public Map<Integer, GoodsTB> getGoodsInfoMap() {
		return goodsInfoMap;
	}


	public void setGoodsInfoMap(Map<Integer, GoodsTB> goodsInfoMap) {
		this.goodsInfoMap = goodsInfoMap;
	}









	
	

}
